import request from '@/utils/request.ts';
import type {
  SpuListReqData,
  SpuListResData,
  SpuInfoResData,
  AllTrademarkResData,
  BaseSaleAttrListResData,
  SpuSaleAttrListResData,
  SpuInfo,
  SkuListResData,
  SpuImageListResData,
  SkuInfo
} from './type.ts';
import { ResponseData } from '../type.ts';

enum API {
  GET_SPU_LIST = '/admin/product',
  GET_SPU_INFO = '/admin/product/getSpuInfo',
  GET_ALL_TRADEMARK = '/admin/product/baseTrademark/getTrademarkList',
  GET_BASE_SALEATTRLIST = '/admin/product/baseSaleAttrList',
  GET_SPU_SALEATTRLIST = '/admin/product/spuSaleAttrList',
  SAVE_SPU_INFO = '/admin/product/saveSpuInfo',
  UPDATE_SPU_INFO = '/admin/product/updateSpuInfo',
  GET_SKULIST_BYSPUID = '/admin/product/findBySpuId',
  GET_SPU_IMAGELIST = '/admin/product/spuImageList',
  SAVE_SKU_INFO = '/admin/product/saveSkuInfo',
  DELETE_SPU = '/admin/product/deleteSpu'
}

// 获取Spu列表
export const reqGetSpuList = ({ page, limit, category3Id }: SpuListReqData) =>
  request.get<any, SpuListResData>(
    `${API.GET_SPU_LIST}/${page}/${limit}?category3Id=${category3Id}`
  );

// 获取spu详情
export const reqGetSpuInfo = (spuId: number) =>
  request.get<any, SpuInfoResData>(`${API.GET_SPU_INFO}/${spuId}`);

// 获取全部可选品牌列表
export const reqGetAllTrademark = () =>
  request.get<any, AllTrademarkResData>(API.GET_ALL_TRADEMARK);

// 获取基础销售属性列表
export const reqGetBaseSaleAttrList = () =>
  request.get<any, BaseSaleAttrListResData>(API.GET_BASE_SALEATTRLIST);

// 获取spu销售属性列表
export const reqGetSpuSaleAttrList = (spuId: number) =>
  request.get<any, SpuSaleAttrListResData>(
    `${API.GET_SPU_SALEATTRLIST}/${spuId}`
  );

// 提交spu信息
export const reqSaveSpuInfo = (data: SpuInfo) => {
  const url = data.id ? API.UPDATE_SPU_INFO : API.SAVE_SPU_INFO;
  return request.post<any, ResponseData>(url, data);
};

// 获取sku列表
export const reqGetSkuListBySpuId = (spuId: number) =>
  request.get<any, SkuListResData>(`${API.GET_SKULIST_BYSPUID}/${spuId}`);

// 获取spu图片列表
export const reqGetSpuImageList = (spuId: number) =>
  request.get<any, SpuImageListResData>(`${API.GET_SPU_IMAGELIST}/${spuId}`);

// 提交sku信息
export const reqSaveSkuInfo = (data: SkuInfo) =>
  request.post<any, ResponseData>(API.SAVE_SKU_INFO, data);

// 删除spu
export const reqDeleteSpu = (spuId: number) =>
  request.delete<any, ResponseData>(`${API.DELETE_SPU}/${spuId}`);
